package com.primary.controller;

import com.primary.common.Result;
import com.primary.controller.check.checklogic.UsersCheckLogic;
import com.primary.entity.Users;
import com.primary.service.UsersService;
import com.primary.controller.check.PhoneAndEmailCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Shen Jianhui
 * @date 2023/07/08 12:56 Saturday
 * @description Provide a Username for a new user who is about to use this system firstly.
 */

@RestController
@RequestMapping("register")
public class RegisterController {

    @Autowired
    UsersService usersService;
    /**
     * @author Shen Jianhui
     * @date 2023/07/08 Saturday
     * @updateDate 2023/07/27 Thursday
     * @updateDate 2023/08/06 Sunday
     * @description Provide a Username for a new user who is about to use this system firstly.
     * @updateDescription After the postman to test this requesting, there are many issues remain to be resolved,
     *                    so this requesting has been added StringUtils class to avoid one/some information is/are blank
     *                    or null. At the same time, the Service interface will return some different results, there are many
     *                    code and message to accept them.
     * @updateDescription In the Software System, most operations should be focus on Service but not Controller. Actually if
     *                    there are too many service on Controller, the system structure will become irrational, so it is necessary
     *                    to let some operations to other classes like Dto or DtoCheck.
     * @return The status of this System response
     */
    @RequestMapping(value = "/usersreg", method = {RequestMethod.POST})
    public Result registerByNameAndPwd(@RequestBody Users users) {
        //UpdateDate 2023/08/06 Sunday Adding CheckLogic to simplify the code START.
        if (!UsersCheckLogic.userDtoCheckLogic(users)) {
            return Result.error("-110", "用户名密码只能是字母或数字");
        //UpdateDate 2023/08/06 Sunday Adding CheckLogic to simplify the code END.
        } else {
            if (null != usersService.getNameForRegister(users.getName())) {
                return Result.error("用户已存在");
            } else if (!PhoneAndEmailCheck.isPhone(users.getPhone()) || !PhoneAndEmailCheck.isEmail(users.getEmail())) {
                return Result.error("-101", "电话号码或邮箱格式不正确");
            } else if (9 == usersService.registerByNameAndPwd(users)) {
                return Result.error("-100", "电话号码或邮箱已经存在");
            } else {
                return Result.success("注册成功!");
            }
        }
    }
}
